using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Meta.Editor
{
	/// <summary>
	/// Summary description for FindDialog.
	/// </summary>
	public class FindDialog : System.Windows.Forms.Form
	{
		public System.Windows.Forms.TextBox searchText;
		private System.Windows.Forms.Button okButton;
		private System.Windows.Forms.Button cancelButton;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		CodeBox code;

		public FindDialog(CodeBox code)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.code = code;
			string selection = code.SelectedText;
			if (selection.Length==0)
				searchText.Text = lastSearch;
			else
				searchText.Text = selection;

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		public static string lastSearch="";

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.searchText = new System.Windows.Forms.TextBox();
			this.okButton = new System.Windows.Forms.Button();
			this.cancelButton = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// searchText
			// 
			this.searchText.Location = new System.Drawing.Point(8, 8);
			this.searchText.Name = "searchText";
			this.searchText.Size = new System.Drawing.Size(352, 20);
			this.searchText.TabIndex = 0;
			this.searchText.Text = "";
			// 
			// okButton
			// 
			this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.okButton.Location = new System.Drawing.Point(208, 40);
			this.okButton.Name = "okButton";
			this.okButton.TabIndex = 1;
			this.okButton.Text = "OK";
			this.okButton.Click += new System.EventHandler(this.okButton_Click);
			// 
			// cancelButton
			// 
			this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cancelButton.Location = new System.Drawing.Point(288, 40);
			this.cancelButton.Name = "cancelButton";
			this.cancelButton.TabIndex = 2;
			this.cancelButton.Text = "Cancel";
			this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
			// 
			// FindDialog
			// 
			this.AcceptButton = this.okButton;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.cancelButton;
			this.ClientSize = new System.Drawing.Size(368, 70);
			this.Controls.Add(this.cancelButton);
			this.Controls.Add(this.okButton);
			this.Controls.Add(this.searchText);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FindDialog";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Find text";
			this.ResumeLayout(false);

		}
		#endregion

		private void okButton_Click(object sender, System.EventArgs e) {
			lastSearch = searchText.Text;
			code.HideSelection = false;
			if (code.Find(searchText.Text, code.SelectionStart+code.SelectionLength, RichTextBoxFinds.None)<0)
				cancelButton.Select();
		}

		private void cancelButton_Click(object sender, System.EventArgs e) {
			code.HideSelection = true;
			Close();
		}
	}
}
